package com.sywb.chuangyebao.view;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sywb.chuangyebao.R;
import com.sywb.chuangyebao.contract.AnswerDetailContract;
import com.sywb.chuangyebao.utils.RegexUtils;
import com.sywb.chuangyebao.utils.Utils;

import org.bining.footstone.utils.SoftKeyboardUtils;
import org.bining.footstone.utils.ToastUtils;
import org.bining.footstone.widget.DrawableEditText;
import org.bining.footstone.widget.PullToRefreshView;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 回答详情
 */
public class AnswerDetailActivity extends ActionbarActivity<AnswerDetailContract.Presenter> implements AnswerDetailContract.AnswerDetail {

    @BindView(R.id.common_recycler)
    RecyclerView commonRecycler;
    @BindView(R.id.common_refresh)
    PullToRefreshView commonRefresh;
    @BindView(R.id.common_nodata_content)
    TextView commonNodataContent;
    @BindView(R.id.common_nodata_icon)
    ImageView commonNodataIcon;
    @BindView(R.id.common_nodata_subtitle)
    TextView commonNodataSubtitle;
    @BindView(R.id.common_nodata)
    RelativeLayout commonNodata;
    @BindView(R.id.et_comment)
    DrawableEditText etComment;
    @BindView(R.id.iv_comment)
    ImageView ivComment;
    @BindView(R.id.tv_comment_count)
    TextView tvCommentCount;
    @BindView(R.id.iv_zan)
    ImageView ivZan;
    @BindView(R.id.tv_zan_count)
    TextView tvZanCount;
    @BindView(R.id.rl_comment_zan)
    RelativeLayout rlCommentZan;
    @BindView(R.id.tv_answer_next)
    TextView tvAnswerNext;

    private int answerId;
    private int nextAnswerId;

    @Override
    public int getLayoutId(Bundle savedInstanceState) {
        return R.layout.activity_answer_detail;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        if (savedInstanceState == null) {
            answerId = getIntent().getIntExtra("p0", 0);
        } else {
            answerId = savedInstanceState.getInt("p0");
        }

        setTitle(R.string.answer_detail);
        commonRefresh.setBackgroundColor(ContextCompat.getColor(mActivity, R.color.colorLight));

        etComment.setOnFocusChangeListener(new android.view.View.
                OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    if (!Utils.checkIsNext(getMyFragmentManager())) {
                        etComment.clearFocus();
                        SoftKeyboardUtils.stopSoftKeybord(etComment);
                        return;
                    }
                    // 此处为得到焦点时的处理内容
                } else {
                    // 此处为失去焦点时的处理内容
                }
            }
        });

        etComment.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (!TextUtils.isEmpty(editable.toString()) && rlCommentZan.getVisibility() == View.VISIBLE) {
                    rlCommentZan.setVisibility(View.GONE);
                    tvAnswerNext.setText(R.string.publish);
                    tvAnswerNext.setBackgroundColor(Color.WHITE);
                    tvAnswerNext.setTextColor(ContextCompat.getColor(mActivity, R.color.colorTheme));
                    tvAnswerNext.setPadding(0, 0, 0, 0);
                } else if (TextUtils.isEmpty(editable.toString()) && rlCommentZan.getVisibility() != View.VISIBLE) {
                    rlCommentZan.setVisibility(View.VISIBLE);
                    tvAnswerNext.setTextColor(ContextCompat.getColor(mActivity, R.color.colorDark));
//                    tvAnswerNext.setText(R.string.next_answer);
                    tvAnswerNext.setText(nextAnswerId > 0 ? R.string.next_answer : R.string.last_answer);
                    tvAnswerNext.setBackgroundResource(R.drawable.round_line_gray_bg);

                    int dp6 = mActivity.getResources().getDimensionPixelSize(R.dimen.dp_6);
                    int dp10 = mActivity.getResources().getDimensionPixelSize(R.dimen.dp_10);
                    tvAnswerNext.setPadding(dp10, dp6, dp10, dp6);
                }
            }
        });

        addMenuImageItme(R.drawable.news_more_normal, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mPresenter.showDialog();
            }
        });
    }

    @Override
    public void initPresenter() {
        mPresenter.initPresenter(this);
        mPresenter.initAnswerData(answerId);
    }

    @OnClick({R.id.iv_comment, R.id.tv_answer_next, R.id.iv_zan})
    public void onClick(View view) {
        if (!isCanClick(view)) return;
        switch (view.getId()) {
            case R.id.iv_comment:
                commonRecycler.scrollToPosition(1);
                break;
            case R.id.tv_answer_next:
                if (rlCommentZan.getVisibility() == View.VISIBLE) {
                    //下一个回答
//                    mPresenter.goNextAnswer(sort, currentAnswerIndex);
                    if (nextAnswerId > 0) {
                        advance(AnswerDetailActivity.class, nextAnswerId);
                    } else {
                        ToastUtils.show(mActivity, "这是最后一个回答");
                    }
                } else {
                    //发布评论
                    mPresenter.publish(etComment);
                }
                break;
            case R.id.iv_zan:
                mPresenter.zanAnswer();
                break;
        }
    }

//    @Override
//    protected void onStop() {
//        super.onStop();
////        Logger.e("onStop");
//        mPresenter.playStop();
//    }

    @Override
    protected void onPause() {
        super.onPause();
//        Logger.e("onPause");
        mPresenter.playStop();
    }


    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putLong("p0", answerId);
    }

    public int getPageNum() {
        return 1;
    }

    @Override
    public PullToRefreshView getPullToRefreshView() {
        return commonRefresh;
    }

    @Override
    public RecyclerView getRecyclerView() {
        return commonRecycler;
    }

    @Override
    public void setNoData(boolean isNoData) {
//        commonNodata.setVisibility(isNoData ? View.VISIBLE : View.GONE);
    }

    @Override
    public int getDialogViewHeight() {
        return actionbar.getMeasuredHeight() + content.getMeasuredHeight();
    }

    @Override
    public void setLikeCount(int likeCount) {
        tvZanCount.setText(RegexUtils.getFormatNum(likeCount));
        tvZanCount.setVisibility(likeCount > 0 ? View.VISIBLE : View.GONE);
    }

    @Override
    public void setCommentCount(int commentCount) {
        etComment.setText("");
        Utils.hideSoftKeyborad(mActivity, etComment);
        tvCommentCount.setText(RegexUtils.getFormatNum(commentCount));
        tvCommentCount.setVisibility(commentCount > 0 ? View.VISIBLE : View.GONE);
    }

    @Override
    public long getAnswerId() {
        return answerId;
    }

    @Override
    public void setZanChanged(boolean isZan) {
        ivZan.setImageResource(isZan ? R.drawable.praise_red_big : R.drawable.praise_bb);
    }

    @Override
    public void setNextAnswer(int nextAnswerId) {
        this.nextAnswerId = nextAnswerId;
//        tvAnswerNext.setTextColor(ContextCompat.getColor(mActivity, nextAnswerId > 0 ? R.color.colorDark : R.color.colorButtonHint));
        tvAnswerNext.setText(nextAnswerId > 0 ? R.string.next_answer : R.string.last_answer);
    }
}
